package natappvip.liujinliang.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import natappvip.liujinliang.util.FileHandler;
import natappvip.liujinliang.util.PropertiesUtil;

/**
 * @ClassName:  UploadServlet   
 * @Despt: 上传文件服务端处理类  
 * @Auth: Liujinliang 
 * @Date: 2018年7月29日 下午9:41:07
 */
public class UploadServlet extends BaseControllerServlet {
	
	/**   
	 * @Fields serialVersionUID : 判断序列化与反序列化是否来自于同一个类 
	 */ 
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding(PropertiesUtil.getProperty("charset"));
		response.setCharacterEncoding(PropertiesUtil.getProperty("charset"));
		
		/**
		 * enctype="multipart/form-data"
		 * 不能使用request.getParameter()及相关方法获取
		 * 所以需要自己去解析这段代码
		 * ------WebKitFormBoundary6TAB8KxvuJTZYfUn
		 * Content-Disposition: form-data; name="name"
		 * 
		 * Test.txt
		 * ------WebKitFormBoundary6TAB8KxvuJTZYfUn
		 * Content-Disposition: form-data; name="size"
		 * 
		 * 45
		 * ------WebKitFormBoundary6TAB8KxvuJTZYfUn
		 * Content-Disposition: form-data; name="content"
		 * 
		 * 111
		 * 222
		 * 333
		 * ……
		 * 
		 * ------WebKitFormBoundary6TAB8KxvuJTZYfUn--
		 */
		String saveInfo = fileSourceService.save(FileHandler.getByteArray(request.getInputStream()));
		String mdata = fileMetaDataLocalService.record(saveInfo);
		
		if (mdata != null) {
			response.getWriter().println("<h1>保存成功 </h1>" );
		} else {
			response.getWriter().println("<h1>保存失败</h1>");
		}
	}
}
